package com.ygqh.baby.service.impl;
import java.util.List;

import  com.ygqh.baby.ao.QueryInfo;
import  com.ygqh.baby.ao.ResultSet;
import  com.ygqh.baby.po.AdminMaterial;
import  com.ygqh.baby.mapper.AdminMaterialMapper;
import  com.ygqh.baby.service.AdminMaterialService;

import  org.springframework.util.Assert;

import java.util.Arrays;

import  org.springframework.stereotype.Service;
import  org.springframework.beans.factory.annotation.Autowired;

import  com.ygqh.baby.po.AdminMaterialExample;
import  com.ygqh.baby.ao.DataStatus;

@Service
public class AdminMaterialServiceImpl implements AdminMaterialService{
	
	@Autowired
	private AdminMaterialMapper adminMaterialMapper;
	
	@Override
	public List<AdminMaterial> find() {
		AdminMaterialExample example = new AdminMaterialExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminMaterialMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		AdminMaterialExample example = new AdminMaterialExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminMaterialMapper.countByExample(example);
	}

	@Override
	public AdminMaterial findById(Long id) {
		return adminMaterialMapper.selectByPrimaryKey(id);
	}


	

	@Override
	public int save(AdminMaterial adminMaterial) {
		return adminMaterialMapper.insertSelective(adminMaterial);
	}

	@Override
	public int update(AdminMaterial adminMaterial) {
		return adminMaterialMapper.updateByPrimaryKeySelective(adminMaterial);
	}

	@Override
	public ResultSet<AdminMaterial> search(QueryInfo queryInfo,String q) {
		List<AdminMaterial>  list =  adminMaterialMapper.selectSuper(queryInfo, q);
		int count = adminMaterialMapper.countSuper(q);
		ResultSet<AdminMaterial> resultSet = new ResultSet<AdminMaterial>(count,list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		AdminMaterialExample example = new AdminMaterialExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		AdminMaterial record = new AdminMaterial();
		record.setStatus(DataStatus.Delete);

		return adminMaterialMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(AdminMaterial adminMaterial) {
		int r = 0;
		if(adminMaterial.getId() != null) {
			r = this.update(adminMaterial);
		}else {
			r = this.save(adminMaterial);
		}
		return r;
	}

	@Override
	public int findName(String name, Long id) {
	  return adminMaterialMapper.selectByName(name, id);
	}

	
}

